<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Db;

class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {

        return $this->view->fetch();
    }

    public function search()
    {
        if($this->request->isPost()){
            $username = input("username");
            $page = input("page") ? input("page") : 0;
            if(empty($username)){
                return json(["status"=>"401","msg"=>"请输入用户名"]);
            }

            $cacheTime = 10;
            $username = input("username");
            $user = Db::name("vip_user")->where("username",$username)->find();
            //dump(Db::name("vip_user")->getLastSql());
            //dump($user);
            if(empty($user)){
                return json(["status"=>401,"msg"=>"请输入正确的用户名"]);
            }
            // 用户数据
            $data["user"]["username"] = $user["username"];
            $data["user"]["level"] = $user["level"];
            $data["user"]["money"] = $user["money"];
            //$data["user"]["createtime"] = date("Y-m-d",$user["createtime"]);
            $data["user"]["updatetime"] = date("Y-m-d H:i",$user["updatetime"]);
            $nextSave_money = Db::name("vip_description")->where("level",$data["user"]["level"] + 1)->value("save_money");
            //dump(Db::name("vip_description")->getLastSql());
            $data["user"]["next_save_money"] = ($nextSave_money * 100 - $data["user"]["money"] * 100) / 100;
            //dump($nextSave_money);
            //3000000.00
            //dump($data["user"]);
            //die;
            // 晋级奖金数据
            $jiangli = Db::name("vip_jiangli")->where("username",$username)->order("level desc")->cache($cacheTime)->limit($page,4)->select();
            //$data["jiangli"]["username"] = isset($jiangli["username"]) ? $jiangli["username"] : "";
            //$data["jiangli"]["level"] = isset($jiangli["level"]) ? $jiangli["level"] : "";
            //$data["jiangli"]["status"] = isset($jiangli["status"]) && $jiangli["status"] == 1 ? "已发放" : "未发放";
            // 好运金数据
            $haoyun = Db::name("vip_haoyun")->where("username",$username)->order("id asc")->cache($cacheTime)->limit($page,4)->select();
            //$haoyun["haoyun"]["username"] = isset($haoyun["username"]) ? $haoyun["username"] : "";
            //$haoyun["haoyun"]["username"] = isset($haoyun["username"]) ? $haoyun["username"] : "";
            return json(["status"=>200,"msg"=>"ok","data"=>["user"=>$data["user"],"jiangli"=>$jiangli,"haoyun"=>$haoyun]]);
        }
    }
}
